﻿using System;
using System.Net.Sockets;
using System.Text;
using System.Data;
using Anchor.Miscellaneous;

namespace Anchor.Sockets
{
    class SocketRequest
    {
        private Socket Socket;
        public int SocketID;
        private string IPAddress;
        byte[] data = new byte[2000];

        public void Invoke(int SockID, Socket Sock)
        {
            //Init the Connections diction
            this.Socket = Sock;
            this.SocketID = SockID;
            this.IPAddress = Sock.RemoteEndPoint.ToString().Split(':')[0];

            //Hai!
            using (DatabaseClient dbClient = Environment.GetDatabase().GetClient())
            {
                if (Environment.Configuration["hello.socket"].Contains("OLDSCHOOL"))
                    SendMessage("@@" + Convert.ToChar(1));

                else if (Environment.Configuration["hello.socket"].Contains("FLASH"))
                    SendMessage("<?xml version=\"1.0\"?>" + "\r\n" + "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">" + "\r\n" + "<cross-domain-policy>" + "\r\n" + " <allow-access-from domain=\"*\" to-ports=\"1-40000\" />" + "\r\n" + " <allow-access-from domain=\"*\" to-ports=\"1-40000\" />" + "\r\n" + " <allow-access-from domain=\"\" to-ports=\"1-40000\" />" + "\r\n" + "</cross-domain-policy>" + Convert.ToChar(0));

            }

            //Start receiving
            this.Socket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(Recieve), Socket);
        }

        private void Recieve(IAsyncResult iAr)
        {
            Socket Remote = (Socket)iAr.AsyncState;
            int recv = Remote.EndReceive(iAr);

            string ClientData = Encoding.ASCII.GetString(data, 0, recv);

            if (ClientData.Length > 0)
            {
                ClientData = ClientData.Substring(3);
                Console.WriteLine("[{0}] --> {1}", SocketID, ClientData);
                //Start static packet handling

                //Invoke packets
                if (ClientData.Contains("file") == false)
                {
                    Packets.Invoke(this, hEncoding.decodeB64(ClientData.Substring(0, 2)), ClientData);
                }

            }

            this.Socket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(Recieve), Socket);

        }
        private void SendData(byte[] sData)
        {
            this.Socket.BeginSend(sData, 0, sData.Length, SocketFlags.None, new AsyncCallback(SendData), this.Socket);
        }
        private void SendData(IAsyncResult iar)
        {
            Socket server = (Socket)iar.AsyncState;
            int sent = server.EndSend(iar);
        }
        public void SendMessage(string Message)
        {
            try
            {
                Console.WriteLine("--> " + Message.Replace("{0}", "\x00").Replace("{1}", "\x01").Replace("{2}", "\x02").Replace("{3}", "\x03").Replace("{4}", "\x04").Replace("{5}", "\x05").Replace("{6}", "\x06").Replace("{7}", "\x07").Replace("{8}", "\x08").Replace("{9}", "\x09").Replace("{10}", "\x010").Replace("{11}", "\x011").Replace("{12}", "\x012").Replace("{13}", "\r"));
                byte[] buff = Encoding.ASCII.GetBytes(Message.Replace("{0}", "\x00").Replace("{1}", "\x01").Replace("{2}", "\x02").Replace("{3}", "\x03").Replace("{4}", "\x04").Replace("{5}", "\x05").Replace("{6}", "\x06").Replace("{7}", "\x07").Replace("{8}", "\x08").Replace("{9}", "\x09").Replace("{10}", "\x010").Replace("{11}", "\x011").Replace("{12}", "\x012").Replace("{13}", "\r"));
                this.SendData(buff);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
